//
//  JingYingTableViewCell.m
//  EnterpriseEdition
//
//  Created by ypj on 2021/10/8.
//

#import "JingYingTableViewCell.h"
#import "QuanXianModel.h"
#import "HomeCategoryItemCell.h"

#define kItemHeight 70

#define kItemCount 4

@interface JingYingTableViewCell ()<UICollectionViewDelegate,UICollectionViewDataSource>

@property (nonatomic) UICollectionView *collectionView;

@end


@implementation JingYingTableViewCell


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];

    
    if (self) {
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
        
        flowLayout.itemSize = CGSizeMake(kScreenWidth/kItemCount,kItemHeight);
        flowLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
        flowLayout.minimumLineSpacing= 0;
        flowLayout.minimumInteritemSpacing = 0;
        
        self.collectionView.showsVerticalScrollIndicator = NO;
        self.collectionView.showsHorizontalScrollIndicator = NO;
        self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:flowLayout];
        self.collectionView.delegate = self;
        self.collectionView.dataSource = self;
        self.collectionView.backgroundColor = UIColor.whiteColor;
        [self.contentView addSubview:self.collectionView];
        
        self.collectionView.scrollEnabled = NO;
        self.collectionView.frame = CGRectMake(0, 0, kScreenWidth, 30);
        [self.collectionView registerClass:[LXJingYingCollectionCell class] forCellWithReuseIdentifier:NSStringFromClass([LXJingYingCollectionCell class])];
        [self.collectionView registerClass:[UICollectionReusableView  class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:NSStringFromClass([UICollectionReusableView class])];
        [self.collectionView registerNib:[UINib nibWithNibName:NSStringFromClass([HomeCategoryItemCell  class]) bundle:nil] forCellWithReuseIdentifier:NSStringFromClass([HomeCategoryItemCell class])];

        

    }
    return self;
}



- (void)setDataAry:(NSArray *)dataAry{

    _dataAry = dataAry;

    CGFloat height = [JingYingTableViewCell getCellHeightWithDataAry:dataAry];
    self.collectionView.height = height;
    [self.collectionView reloadData];

}

+(CGFloat)getCellHeightWithDataAry:(NSArray*)dataAry
{
    NSInteger totalCount = 0;
    CGFloat sectionHeight = 0;
    for (QuanXianModel *model in dataAry) {
        totalCount += [model.children count];
        sectionHeight += model.height;
    }
    CGFloat height = totalCount/kItemCount *kItemHeight;
    if (totalCount%kItemCount !=0 ){
        height = (totalCount/kItemCount +1) *kItemHeight ;
    }
    return height + sectionHeight;
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return self.dataAry.count;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    QuanXianModel *model = self.dataAry[section];
    return [model.children count];
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    
    QuanXianModel *model = self.dataAry[indexPath.section];
    QuanXianSmallModel *item = model.children[indexPath.row];
    
    // 没有图片,表示是代办事项或者经营状况
    if (model.isNotUrl == 1) {
        LXJingYingCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([LXJingYingCollectionCell class]) forIndexPath:indexPath];
        cell.topLabel.text = [NSString stringWithFormat:@"%@",item.remindNum];
        cell.bottomLabel.text = [NSString stringWithFormat:@"%@",item.name];
        return cell;
    }
    else{
        HomeCategoryItemCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([HomeCategoryItemCell class]) forIndexPath:indexPath];
        cell.titleLb.text = [NSString stringWithFormat:@"%@",item.name];
        [cell.iconImage sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",HTTP_URL,[NSString isChinese:item.icon]]] placeholderImage:Image(@"default_icon")];
        return cell;
    }
    
    
}



- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    
    QuanXianModel *model = self.dataAry[indexPath.section];
    QuanXianSmallModel *item = model.children[indexPath.row];
    if (self.ToSendValue) {
        self.ToSendValue(item);
    }
    
}


- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        UICollectionReusableView *headerV = (UICollectionReusableView *)[collectionView dequeueReusableSupplementaryViewOfKind: UICollectionElementKindSectionHeader withReuseIdentifier:NSStringFromClass([UICollectionReusableView class]) forIndexPath:indexPath];
        [headerV setClipsToBounds:YES];
        
        for (UIView *v in headerV.subviews) {
            [v removeFromSuperview];
        }
        QuanXianModel *model = self.dataAry[indexPath.section];

        UILabel *titleNameLb=[[UILabel alloc] initWithFrame:CGRectMake(15, 0, ScreenWidth-30, model.height)];
        titleNameLb.textColor=Color333;
        titleNameLb.font= Fond15;
        titleNameLb.text = model.name;

       [headerV addSubview:titleNameLb];
        return headerV;
    }
    return nil;
    
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    QuanXianModel *model = self.dataAry[section];
    return CGSizeMake(ScreenWidth, model.height);
}

+(NSString *)reuseIdentifier{
    return @"JingYingTableViewCell";
}

@end
@implementation LXJingYingCollectionCell

-(instancetype)initWithFrame:(CGRect)frame{
    self=[super initWithFrame:frame];
    if (self){
        [self setUI];
    }
    return self;
}

- (void)setUI{
    
    self.topLabel=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.width, self.height/2)];
    self.topLabel.textColor= [UIColor colorWithHexString:@"#3C6CFC"];
    self.topLabel.font= Fond20;
    self.topLabel.font = [UIFont fontWithName:@"PingFang-SC-Medium" size:20];
    self.topLabel.textAlignment = NSTextAlignmentCenter;
   [self addSubview:self.topLabel];
    
    self.bottomLabel=[[UILabel alloc] initWithFrame:CGRectMake(0, MaxY(self.topLabel), self.width, self.height/2)];
    self.bottomLabel.textColor= Color999;
    self.bottomLabel.font= Fond13;
    self.bottomLabel.textAlignment = NSTextAlignmentCenter;
   [self addSubview:self.bottomLabel];
    
}




@end


